from medirecord import medirecord_blue
from database.database import MedicalRecord,db,User,MedicalRecordMedicine
from flask import request,jsonify,session
from datetime import datetime


# 病人选择医生，开启诊疗记录
@medirecord_blue.route('/create', methods=['POST'])
def add_medirecord():
    data = request.get_json()
    # 通过session获取patient_id
    patient_name = session.get('username')
    patient = User.query.filter_by(username=patient_name).first()
    patient_id = patient.id
    doctor_id = data.get('doctor_id')
    medical_record = MedicalRecord(patient_id=patient_id,doctor_id=doctor_id,post_time=datetime.now(),disease_id=1)
    db.session.add(medical_record)
    db.session.commit()
    return jsonify({'code':200,'msg':'success','data':None})


# 患者获取自己的基础诊疗记录
@medirecord_blue.route('/patient', methods=['GET'])
def get_medirecord_by_patient():
    patient_name = session.get('username')
    patient = User.query.filter_by(username=patient_name).first()
    patient_id = patient.id
    # 按照时间倒序排列
    medical_records = MedicalRecord.query.filter_by(patient_id=patient_id).order_by(MedicalRecord.post_time.desc()).all()
    medical_record_list = []
    for medical_record in medical_records:
        # 创建对象
        obj = {
            'id':medical_record.id,
            'doctor_id':medical_record.doctor_id,
            'post_time':medical_record.post_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        medical_record_list.append(obj)
    return jsonify({'code':200,'msg':'success','data':medical_record_list})


# 医生获取自己的诊疗记录
@medirecord_blue.route('/doctor', methods=['GET'])
def get_medirecord_by_doctor():
    doctor_name = session.get('username')
    doctor = User.query.filter_by(username=doctor_name).first()
    doctor_id = doctor.id
    # 按照时间倒序排列
    medical_records = MedicalRecord.query.filter_by(doctor_id=doctor_id).order_by(MedicalRecord.post_time.desc()).all()
    medical_record_list = []
    for medical_record in medical_records:
        # 创建对象
        obj = {
            'id':medical_record.id,
            'patient_id':medical_record.patient_id,
            'post_time':medical_record.post_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        medical_record_list.append(obj)
    return jsonify({'code':200,'msg':'success','data':medical_record_list})


# 医生完善诊疗记录
@medirecord_blue.route('/fill', methods=['POST'])
def fill_medirecord():
    data = request.get_json()
    print(data)
    id = data.get('id')
    medical_record = MedicalRecord.query.filter_by(id=id).first()
    if medical_record is None:
        return jsonify({'code':400,'msg':'未找到该记录','data':None})
    medical_record.disease_id = data.get('disease_id')
    medical_record.info= data.get('info')
    medical_record.treatment = data.get('treatment')
    medical_record.end_time = datetime.strptime(data.get('end_time'),'%Y-%m-%d %H:%M:%S')

    # 根据id修改诊疗记录
    db.session.add(medical_record)
    db.session.commit()
    return jsonify({'code':200,'msg':'success','data':None})


# 医生开药
@medirecord_blue.route('/medicine', methods=['POST'])
def add_medicine():
    data = request.get_json()
    id = data.get('id')
    medicine_list = data.get('medicine')
    count_list = data.get('count')
    for i in range(len(medicine_list)):
        tmp = MedicalRecordMedicine(medical_record_id=id,medicine_id=medicine_list[i],count=count_list[i])
        db.session.add(tmp)
    db.session.commit()
    return jsonify({'code':200,'msg':'success','data':None})



